//
//  HMTabBarController.swift
//  weibo
//
//  Created by 杨念川 on 16/8/29.
//  Copyright © 2016年 杨念川. All rights reserved.
//

import UIKit

class HMTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        //把自定义的tabBar赋给系统的tabBar
       let tabBar = HMTabBar()
        //定义一个闭包
        let closure = { [weak self] in
            print("我点击了组合按钮\(self)")
        }
        //将闭包传给tabBar
        tabBar.composeButtonClosure = closure
        //给系统的tabBar赋值
        self .setValue(tabBar, forKey: "tabBar")
        addChildViewControllers()
    }
    //添加到子控制器
    func addChildViewControllers() {
        addChildViewController(createViewController(vc: HMHomeController(), title: "首页", imageName: "tabbar_home"))
        addChildViewController(createViewController(vc: HMMessageController(), title: "消息", imageName: "tabbar_message_center"))
        addChildViewController(createViewController(vc: HMDiscoverController(), title: "发现", imageName: "tabbar_discover"))
        addChildViewController(createViewController(vc: HMProfileController(), title: "我", imageName: "tabbar_profile"))
    }
    //快速创建控制器
    func createViewController(vc: UIViewController,title: String,imageName: String) -> UIViewController {
        //设置title
        vc.title = title
        //设置图片
        vc.tabBarItem.image = UIImage(named: imageName)
        vc.tabBarItem.selectedImage = UIImage(named: "\(imageName)_selected")
        //设置字体颜色
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orange], for: UIControlState.selected)
        //包装navigationController
        let nav = HMNavigationController(rootViewController: vc)
        return nav
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
